<?php
class ControllerProductCart extends Controller
{
	public function index()
	{
		$this->data['cart'] = unserialize($this->session->data['cart']);
		if(!is_array($this->data['cart'])) $this->data['cart'] = array();
		$this->id="cart";
		$this->template="product/cart.tpl";
		$this->render();
	}
	
	public function addcart()
	{
		$this->load->model("core/media");
		$productid = $this->request->post['productid'];
		
		if($productid != "")
		{
			$media = $this->model_core_media->getItem($productid);
			
			$cart = unserialize($this->session->data['cart']);
			
			$finish = false;
			if(is_array($cart))
			{
				for($i=0;$i<count($cart);$i++)
				{
					if($cart[$i]['productid'] == $productid)
					{
						$cart[$i]['qty'] = (int)$cart[$i]['qty'] + 1;
						$finish = true;
					}
				}
				if($finish == false)
				{
					$cart[] = $this->request->post;
				}
			}elseif(count($media) > 0)
			{
				$cart[] = $this->request->post;
			}
			else
			{
				$this->data['output'] = "Khong tim thay san pham nay!";
			}
			
			$_SESSION['cart'] = serialize($cart);
		}
		$this->id="cart";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function removecart()
	{
		$productid = $this->request->post['productid'];
		$cart = unserialize($this->session->data['cart']);
		$newcart = array();
		if(is_array($cart))
		{
			for($i=0;$i<count($cart);$i++)
			{
				if($cart[$i]['productid'] != $productid)
				{
					$newcart[] = $cart[$i];
				}
			}
		}
		
		$_SESSION['cart'] = serialize($newcart);
		$this->id="cart";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function updatecart()
	{
		$productid = $this->request->post['productid'];
		$cart = unserialize($this->session->data['cart']);
		
		if($productid != "")
		{
			if(is_array($cart))
			{
				for($i=0;$i<count($cart);$i++)
				{
					if($cart[$i]['productid'] == $productid)
					{
						$cart[$i]['qty'] = (int)$this->request->post['qty'];
						if($cart[$i]['qty'] == 0)
						{
							$cart[$i]['qty'] = 1;
						}
					}
				}
			}
			
		}
		$_SESSION['cart'] = serialize($cart);
		$this->id="cart";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function checkout()
	{
		$this->data['cart'] = unserialize($this->session->data['cart']);
		if(!is_array($this->data['cart'])) $this->data['cart'] = array();
		
		$data=$this->request->post;
		$error = $this->validate($data);
		if(count($error))
		{
			foreach($error as $item)
				$this->data['output'].=$item."<br>";
		}
		else
		{
			//Ko co loi
			$this->load->model("addon/order");
			$order['orderdate'] = $this->date->getToday();
			$order['userid'] = "";
			$order['customername'] = $data['fullname'];
			$order['address'] = $data['address'];
			$order['email'] = $data['email'];
			$order['phone'] = $data['phone'];
			$order['status'] = "";
			$order['comment'] = $data['description'];
			$orderid = $this->model_addon_order->insert($order);
			foreach($this->data['cart'] as $item)
			{
				$orderproduct['orderid'] = $orderid;
				$orderproduct['mediaid'] = $item['productid'];
				$orderproduct['quantity'] = $item['qty'];
				$orderproduct['price'] = 0;
				$orderproduct['discount'] = 0;
				$orderproduct['subtotal'] = 0;
				$this->model_addon_order->saveOrderProduct($orderproduct);
			}
			unset($_SESSION['cart']);
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	private function validate($data)
	{
		$err = array();
		if($data['fullname'] == "")
			$err["fullname"] = "Bạn chưa nhập họ và tên";
		if($data['email'] == "")
			$err["email"] = "Email không đươc rỗng";
		else
			if ($this->validation->_checkEmail($data['email']) == false )
				$err["email"] = "Email không đúng định dạng";
		if($data['address'] == "")
			$err["address"] = "Bạn chưa nhập địa chỉ";
		if($data['phone'] == "")
			$err["phone"] = "Bạn chưa nhập số điên thoại";
		if($data['description'] == "")
			$err["description"] = "Bạn chưa nhập lời nhắn";
		return $err;
	}
}
?>